package tienda.zapateria.siszap.da;

import java.sql.Connection;
import tienda.zapateria.siszap.be.BEPersona;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rx.cr.dao.DAOAbstract;

public class DAPersona extends DAOAbstract<BEPersona> {

    public DAPersona(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BEPersona be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_persona");
            setParameterString(be.getNombre());
            setParameterString(be.getApellido());
            setParameterString(be.getDni());
            setParameterString(be.getDireccion());
            setParameterString(be.getSexo());
            setParameterString(be.getUsuario());
            setParameterString(be.getClave());
            setParameterString(be.getEstadocuenta());
            setParameterString(be.getTipo());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEPersona be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_persona");
            setParameterInt(be.getPersonaid());
            setParameterString(be.getNombre());
            setParameterString(be.getApellido());
            setParameterString(be.getDni());
            setParameterString(be.getDireccion());
            setParameterString(be.getSexo());
            setParameterString(be.getUsuario());
            setParameterString(be.getClave());
            setParameterString(be.getEstadocuenta());
            setParameterString(be.getTipo());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEPersona be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_persona");
            setParameterInt(be.getPersonaid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEPersona> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_persona");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEPersona be;
                be = new BEPersona();
                be.setPersonaid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setApellido(getDataString(3));
                be.setDni(getDataString(4));
                be.setDireccion(getDataString(5));
                be.setSexo(getDataString(6));
                be.setUsuario(getDataString(7));
                be.setClave(getDataString(8));
                be.setEstadocuenta(getDataString(9));
                be.setTipo(getDataString(10));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEPersona> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_persona");
            setParameterString("");
            commitc();
            while (existResult()) {
                BEPersona be = new BEPersona();

                be.setPersonaid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setApellido(getDataString(3));
                be.setDni(getDataString(4));
                be.setDireccion(getDataString(5));
                be.setSexo(getDataString(6));
                be.setUsuario(getDataString(7));
                be.setClave(getDataString(8));
                be.setEstadocuenta(getDataString(9));
                be.setTipo(getDataString(10));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEPersona buscar(int i) throws Exception {

        try {
            BEPersona be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_persona_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BEPersona();
                be.setPersonaid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setApellido(getDataString(3));
                be.setDni(getDataString(4));
                be.setDireccion(getDataString(5));
                be.setSexo(getDataString(6));
                be.setUsuario(getDataString(7));
                be.setClave(getDataString(8));
                be.setEstadocuenta(getDataString(9));
                be.setTipo(getDataString(10));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEPersona autenticarUsuario(String user, String password) {
        try {
            BEPersona be = null;
            setStoreProcedure("zapateria.sp_autenticar_usuario");
            setParameterString(user);
            setParameterString(password);
            commitc();
            while (existResult()) {
                be = new BEPersona();
                be.setPersonaid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setApellido(getDataString(3));
                be.setDni(getDataString(4));
                be.setClave(getDataString(5));
                be.setEstadocuenta(getDataString(6));
            }
            return be;
        } catch (Exception e) {
            try {
                rollback(e);
            } catch (Exception ex) {
                Logger.getLogger(DAPersona.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
    }

}
